package com.example.administrator.testxingpiao.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.administrator.testxingpiao.bean.CollectorBean;
import com.example.administrator.testxingpiao.bean.User;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * Created by Administrator on 2016/8/9.
 */
public class ORMLiteCollectHelper extends OrmLiteSqliteOpenHelper{

    private static final String DATA_BASE_NAME = "collectors.db";

    public ORMLiteCollectHelper(Context context) {

        super(context, DATA_BASE_NAME,null,1);
    }

    //创建表格
    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            int state = TableUtils.createTable(connectionSource, CollectorBean.class);
            log("建表状态：" + (state != -1 ? "成功":"失败"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource,CollectorBean.class,true);
            onCreate(database,connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void log(String msg){
        Log.i("TAG",msg);
    }
}
